<!DOCTYPE html>
<html lang="en"><!-- InstanceBegin template="/Templates/MainTemplate.dwt" codeOutsideHTMLIsLocked="false" -->
  <head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta name="description" content="">
    <meta name="author" content="">
<!-- #BeginEditable "doctitle" -->
<title>Jdon Framework integration with Spring</title>  
<!-- #EndEditable -->
<!-- InstanceBeginEditable name="head" --><!-- InstanceEndEditable -->
    <!-- Bootstrap core CSS -->
     <link rel="stylesheet" href="css/bootstrap.min.css">   

    <!-- Custom styles for this template -->
    <link href="css/starter-template.css" rel="stylesheet">

   <!-- HTML5 shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!--[if lt IE 9]>
      <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
      <script src="https://oss.maxcdn.com/libs/respond.js/1.3.0/respond.min.js"></script>
    <![endif]-->    
  </head>
<body>
 <div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.html">Jdon Framework</a>
        </div>
        <div class="collapse navbar-collapse">
          <ul class="nav navbar-nav">
                <li class="active"><a href="index.html">Home</a></li>
               <li><a href="https://sourceforge.net/projects/jdon/files/">Download</a></li>
               <li><a href="doc.htm" >Document</a></li>
               <li><a href="quickconf.html">Qucik Start</a> </li>
               <li><a href="examples.html">Examples</a></li>
               <li ><a href="https://github.com/banq/jdonframework">Github</a></li>
               <li><a href="articles.html">Articles</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </div>
    

    <div class="container">
     <div class="starter-template">     
    
 <table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">  
  <tr>     
    <td bgcolor="#FFFFFF"  valign="top" align="left">

                  
				<!-- #BeginEditable "main" --> 
                  <h3>How to integrate Jdon Framework  with Spring</h3>
                  <p>　　Jdon Framework can be integration with Spring, so we can use jdonframework's some new features in Spring application. although Jdon is based on PicoContainer that is difference from Spring IOC container, but by a integration class, jdon's component can inject into spring bean and spring bean can too be injected into jdon's component or service or consumer.</p>
                  <p>　　Next step , we show how to write this integration class ?</p>
                  <p>　　There are three hook class in Spring framework:</p>
                  <p>1.<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationContextAware.html"> org.springframework.context.ApplicationContextAware</a>: <br>
                  its setApplicationContext method at first be invoked when spring startup.</p>
                  <p>2.<a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/beans/factory/support/BeanDefinitionRegistryPostProcessor.html">org.springframework.beans.factory.support.BeanDefinitionRegistryPostProcessor</a><br>
                  its postProcessBeanDefinitionRegistry and postProcessBeanFactory methods second be invoked;            </p>
                  <p>3. <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationListener.html">org.springframework.context.ApplicationListener</a><br>
                    its onApplicationEvent method will be noticed after Spring started successfully.
                  </p>
                  <p>　　We using these three class to inject Jdon and spring with each other. at first, we implements <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationContextAware.html">ApplicationContextAware</a> and in its method we startup jdonFramework:</p>
                  <table width="725" border="0">
                    <tr>
                      <td width="719" bgcolor="#CCCCCC"><p>public class AppContextJdon implements <strong>ApplicationContextAware</strong>{</p>
                      <p>/**<br>
* ApplicationContextAware's method<br>
* <br>
* at first run, startup Jdon Framework *<br>
*/<br>
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {<br>
this.applicationContext = applicationContext;</p>
                      <p> 　　if (servletContext == null)<br>
                        　　　　if (applicationContext instanceof WebApplicationContext) {<br>
                        　　　　　　servletContext = ((WebApplicationContext) applicationContext).getServletContext();<br>
                        　　　　　　if (servletContext == null) {<br>
                        　　　　　　　　System.err.print(&quot;this class only fit for Spring Web Application&quot;);<br>
                        　　　　　　　　return;<br>
                        　　　　　　}</p>
                      <p> 　　　　}</p>
                      <p> 　　// start up jdon<br>
                        　　AppContextWrapper acw = new ServletContextWrapper(servletContext);<br>
                        　　ContainerFinder containerFinder = new ContainerFinderImp();<br>
                        　　containerWrapper = containerFinder.findContainer(acw);</p>
                      <p> 　　}</p>
<p>&nbsp;</p>
                      <p>}</p></td>
                    </tr>
                  </table>
                  <p>　　And now we prepare to inject jdon component into spring by implements BeanDefinitionRegistryPostProcessor, here there are two method need override:</p>
                  <table width="725" border="0">
                    <tr>
                      <td width="719" bgcolor="#CCCCCC"><p>public class AppContextJdon implements ApplicationContextAware, <strong>BeanDefinitionRegistryPostProcessor</strong>{</p>
                        <p>　　....</p>
                        <p>/**<br>
* BeanDefinitionRegistryPostProcessor's method<br>
* <br>
* second run: check which spring bean that need injected from Jdon.<br>
* <br>
*/<br>
public void postProcessBeanDefinitionRegistry(BeanDefinitionRegistry registry) throws BeansException {</p>
                        <p> 　　for (String beanName : registry.getBeanDefinitionNames()) {<br>
                          　　　　String beanClassName = beanDefinition.getBeanClassName();<br>
                          　　　　try {<br>
                          　　　　　　Class beanClass = Class.forName(beanClassName);<br>
                          　　　　　　// large project need using Google Collection's lookup<br>
                          　　　　　　for (final Field field : ClassUtil.getAllDecaredFields(beanClass)) {<br>
                          　　　　　　　　if (field.isAnnotationPresent(Autowired.class)) {<br>
                          　　　　　　　　　　// prepare to inject jdon components into spring components with AutoWire;<br>
                          　　　　　　　　　　Object o = findBeanClassInJdon(field.getType());<br>
                          　　　　　　　　　　if (o != null) {<br>
                          　　　　　　　　　　　　neededJdonComponents.put(field.getName(), o);<br>
                          　　　　　　　　　　}<br>
                          　　　　　　　　}<br>
                          　　　　　　}<br>
                          　　　　} catch (ClassNotFoundException ex) {<br>
                          　　　　　　　　ex.printStackTrace();<br>
                          　　　　}<br>
                          　　}<br>
                        }</p>
                        <p> /**<br>
                          * BeanDefinitionRegistryPostProcessor's method<br>
                          * <br>
                          * third run: injecting the jdon component into Spring bean;<br>
                          * <br>
                          */<br>
                          public void postProcessBeanFactory(ConfigurableListableBeanFactory factory) throws BeansException {<br>
                        　　this.factory = factory;</p>
                        <p> 　　//inject jdon components into spring components with AutoWire<br>
                          　　<strong>injectJdonToSpring</strong>();<br>
                          }</p>
                        <p>&nbsp;</p>
                        <p>//inject jdon components into spring components with AutoWire</p>
                        <p>public void <strong>injectJdonToSpring</strong>() {<br>
　　for (String name : neededJdonComponents.keySet()) {<br>
　　　　Object o = neededJdonComponents.get(name);<br>
　　　　if (!factory.containsBean(name))<br>
　　　　　　factory.registerSingleton(name, o);<br>
　　　　}<br>
　　}</p>
                        <p>}</p></td>
                    </tr>
                  </table>
                  <p>　　<strong>injectJdonToSpring</strong>() method will  inject jdon components into spring components with @AutoWire, such as:</p>
                  <p><br>
                    @Controller<br>
                    @RequestMapping(value = &quot;/contact&quot;)<br>
                  public class UserController {</p>
                  <p> 　　@Autowired<br>
                    　　private UserRepository userRepository;</p>
                  <p> 　　@Autowired<br>
                    　　private <strong>RoleAssigner</strong> roleAssigner;</p>
                  <p>}</p>
                  <p><strong>RoleAssigner</strong> is a component registered in Jdon, UserController is SpringMVC's REST controller, so UserController has dependency with com.jdon.domain.dci.RoleAssigner from JdonFramework, so by the method injectJdonToSpring() of above way, we can inject <strong>RoleAssigner</strong> into Spring's UserController.</p>
                  <p>　　Next let us  solve how to inject Spring into Jdon, this step must begin after Spring started and finished. so we implements Spring's <a href="http://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework/context/ApplicationListener.html">ApplicationListener</a></p>
                  <table width="791" border="0">
                    <tr>
                      <td width="785" bgcolor="#CCCCCC"><p>public class AppContextJdon implements ApplicationContextAware, BeanDefinitionRegistryPostProcessor, <strong>ApplicationListener</strong> {</p>
                        <p>　　....</p>
                        <p>/**<br>
* ApplicationListener's method<br>
* <br>
* fouth run: after Spring all bean created and ApplicatioConext is ready .<br>
* <br>
* injecting Spring bean instances into jdon component.<br>
*/<br>
public void onApplicationEvent(ApplicationEvent ae) {<br>
　　if (ae instanceof ContextRefreshedEvent)<br>
　　　　<strong>injectSpringToJdon</strong>();<br>
}</p>
                        <p> public void <strong>injectSpringToJdon</strong>() {<br>
                          　　AutowireCapableBeanFactory beanFactory = applicationContext.getAutowireCapableBeanFactory();<br>
                          　　RegistryDirectory registryDirectory = (RegistryDirectory) 　　containerWrapper.getRegistryDirectory();<br>
                          　　List&lt;String&gt; names = new ArrayList(registryDirectory.getComponentNames());<br>
                          　　for (String name : names) {<br>
                          　　Object o = containerWrapper.lookupOriginal(name);<br>
                          　　beanFactory.autowireBean(o);<br>
                        　　containerWrapper.register(name, o);</p>
                        <p> }</p>
                        <p> }<br>
                        </p>
<p>}</p></td>
                    </tr>
                  </table>
                  <p>&nbsp;</p>
                  <p>　　This step let us can use Spring's component in jdon, such as @Consumer or @Component:</p>
                  <p><br>
                    @Consumer(&quot;saveUser&quot;) //<strong> this is annotation from Jdon</strong><br>
                  public class UserSaveListener implements DomainEventHandler {</p>
                  <p> @Autowired  //<strong>this is annotation from Spring</strong><br>
                  private UserRepository simpleJdbc;</p>
                  <p> public void onEvent(EventDisruptor event, boolean endOfBatch) throws Exception {...}</p>
                  <p>　　When UserSaveListener was fired, and its simpleJdbc can use Spring's datasource or its JDBC template or other resource.</p>
                  <p>　　At last ,donot forget configure this class into Spring XML:</p>
                  <p>&lt;bean id=&quot;appContextJdon&quot; class=&quot;com.jdon.spring.AppContextJdon&quot;&gt;&lt;/bean&gt;</p>
                  <p><a href="AppContextJdon.java">AppContextJdon.java</a></p>
                  <p><a href="http://code.google.com/p/jivejdon/downloads/detail?name=SpringMVC%2BJdon.zip&amp;can=2&amp;q=#makechanges">SpringMVC+Jdon </a></p>
<p>&nbsp;</p>
                   <!-- #EndEditable -->
                 
			  	
          </td>
		  <td width="340" valign="top"  bgcolor="#FFFFD7"> 
          <div class="adsensf" id="vgad336x280">
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 页上左336 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-7573657117119544"
     data-ad-slot="6751585519"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
<script async src="//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js"></script>
<!-- 页上左336 -->
<ins class="adsbygoogle"
     style="display:inline-block;width:336px;height:280px"
     data-ad-client="ca-pub-7573657117119544"
     data-ad-slot="6751585519"></ins>
<script>
(adsbygoogle = window.adsbygoogle || []).push({});
</script>
</div>         
 </td>
  </tr>
</table>


<table width="100%" border="0" cellspacing="0" cellpadding="0" height="2" bgcolor="#000000"  align="center">
              <tr>
                <td></td>
              </tr>
</table>
<table width="100%" border="0" cellspacing="0" cellpadding="0" align="center">
<tr><td height="20" bgcolor="#CAC9BB" align="center">
  
  </td>
</tr>
</table>
 </div>
</div><!-- /.container -->

<!-- InstanceBeginEditable name="EditRegion3" --><!-- InstanceEndEditable -->
        <script src="https://code.jquery.com/jquery-1.10.2.min.js"></script>
		<script src="js/bootstrap.min.js"></script>
<!-- AddThis Smart Layers BEGIN -->
<!-- Go to http://www.addthis.com/get/smart-layers to customize -->
<script type="text/javascript" src="//s7.addthis.com/js/300/addthis_widget.js#pubid=ra-52c4b79515624135"></script>
<script type="text/javascript">
  addthis.layers({
    'theme' : 'transparent',
    'share' : {
      'position' : 'left',
      'numPreferredServices' : 5
    },  
    'whatsnext' : {},  
    'recommended' : {} 
  });
</script>
<!-- AddThis Smart Layers END -->    
</body>
<!-- InstanceEnd --></html>
